// pages/diyform/diyform.js
const app = getApp()
var datas;
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    diydata:"",
    jsonstr:{},
    id:{},
    index:'',
    time:{},
    ordernum:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var _this = this
    _this.setData({
      jsonstr: e.diyform,
      ordernum: e.ordernum,
      index:e.index
    })
    console.log(e);
    datas = JSON.parse(_this.data.jsonstr);
    for (var i = 0; i < datas.length; i++) {
      if (datas[i].types == 'select') {
        var selectdata = datas[i].data.split(',');
        datas[i].selectdata = selectdata;
        if (datas[i].index == '' || datas[i].index == undefined) {
          datas[i].index = 0;
        }

      }
    }
    _this.setData({
      diydata: datas
    });
   /* var jsonstr = '[{"id":"name","name":"姓名","types":"txt","data":"输入姓名","required":true},{"id":"图片","name":"图片","types":"img","data":"2","required":true},{"id":"SEX","name":"性别","types":"select","data":"男,女","required":false},{"id":"简介","name":"简介","types":"txtarea","data":"输入简介","required":false}]';*/

    
  },
  //文本框失去焦点触发
  txtinput:function(e){
    var id = e.target.dataset.id;
    datas[id].val = e.detail.value;
    this.setData({
      diydata: datas
    });
  },
  //选择器触发
  bindActiveChange:function(e){
    var id = e.target.dataset.id;
    datas[id].index = e.detail.value;
    datas[id].val = datas[id].selectdata[e.detail.value];
    this.setData({
      diydata: datas
    });
  },
  //上传图片
  Uploadimg:function(e){
    var id = e.target.dataset.id;
    var counts = e.target.dataset.counts;
    var that = this;
    wx.chooseImage({
      count: counts,  //最多可以选择的图片总数  
      sizeType: ['original', 'compressed'],// 可以指定是原图还是压缩图，默认二者都有 
      success: function (res) {
        var imgpath = Array();
        wx.showLoading({
          title: '',
          icon:'none'
        })
        that.imgupload(res.tempFilePaths, 0, id);
        
        setTimeout(function(){  
         wx.hideLoading();
        }, counts*500);
        
      }
    })
  },
  imgupload: function (tempFilePaths,i,id){
    if(i==0){
      datas[id].val=[];
    }
    if (i < tempFilePaths.length){

    var that = this; 
    var tmppath = tempFilePaths[i];
    wx.uploadFile({
      url: 'https://fastadmin.ranmengwork.cn/api/index/uploadimg.html',
      filePath: tmppath,
      name: 'images',
      formData: {
      },
      success: function (res) {
        var data = JSON.parse(res.data);
        if (data.code == 200) {
          var imgpath = datas[id].val;
          imgpath.push(data.path);
          datas[id].val = imgpath;
          that.setData({
            diydata: datas
          })
        }
        i++;
        that.imgupload(tempFilePaths, i, id);
      }
    });
    }
  },
  //保存全部信息
  save_all:function(){
    var _this = this
    var time = util.formatDate(new Date());
    _this.setData({
      time: time
    });
    for (var i = 0; i < datas.length; i++) {
      if (datas[i].required) {
        if (datas[i].val == "" || datas[i].val == undefined) {
          wx.showToast({
            title: '请填写' + datas[i].name,
            icon: 'none'
          })
          return false;
        }
      }
    }

    //信息录入   
    if (_this.data.index == 0) {
      wx.request({
        url: app.globalData.apiurl + 'api/index/upmodify',
        method: 'GET',
        data: {
          index: _this.data.index,
          ordernum: _this.data.ordernum,
          diyform: datas,
          uid: app.globalData.userInfo.user_id,
          name: app.globalData.name,
          sex: app.globalData.sex,
          idcard: app.globalData.idcard,
          phone: app.globalData.phone
        },
        header: {
          'content-type': 'json'
        },
        success: function (res) {
          console.log(res);
        }
      })
    } else {
      if (_this.data.index =='1') {
        wx.request({
          url: app.globalData.apiurl + 'api/index/upmodify',
          method: 'GET',
          data: {
            index: _this.data.index,
            ordernum: _this.data.ordernum,
            diyform: datas,
            uid: app.globalData.userInfo.user_id,
            name: app.globalData.name,
            sex: app.globalData.sex,
            idcard: app.globalData.idcard,
            phone: app.globalData.phone,
            Dname: app.globalData.changeform[0].Dname,
            Didcard: app.globalData.changeform[1].Didcard,
            Dtel: app.globalData.changeform[2].Dphone
          },
          header: {
            'content-type': 'json'
          },
          success: function (res) {

          }
        })
      }
      if (_this.data.index =='2') {
        wx.request({
          url: app.globalData.apiurl + 'api/index/upmodify',
          method: 'GET',
          data: {
            index: _this.data.index ,
            ordernum: _this.data.ordernum,
            diyform: datas,
            uid: app.globalData.userInfo.user_id,
            name: app.globalData.name,
            sex: app.globalData.sex,
            idcard: app.globalData.idcard,
            phone: app.globalData.phone,
            Dname: app.globalData.changeform[3].name,
            Didcard: app.globalData.changeform[4].idcard,
            Dtel: app.globalData.changeform[5].phone
          },
          header: {
            'content-type': 'json'
          },
          success: function (res) {

          }
        })
      }
    }
    wx.navigateTo({
      url: '../order/order'
    })


  },


  //跳转选择页面
  go_select:function(e){
    var id = e.target.dataset.id;
    var types = e.target.dataset.types;
    datas = this.data.diydata;
    wx.navigateTo({
      url: '/pages/select/select?sel_data=' + datas[id].val + "&types=" + types+"&id="+id+"&all_data="+datas[id].data,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})